package com.example;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
	JSONParser jsonParser = new JSONParser();
	private static final String TAG = "MyService";
	MediaPlayer player;
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	} 
	@Override
	public void onCreate() {
		Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
	}
	@Override
	public void onDestroy() {
		zer.stopp();
		Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
		
		
	}
	Zegarynka zer;
	
	@Override
	public void onStart(Intent intent, int startid) {
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");
		zer=
		new Zegarynka();
		zer.start();
	}
	Context ctx;


	class Zegarynka extends Thread{
		Context mContexx;
		public void stopp(){
			dzialanie=false;
		}
		boolean dzialanie;
		JSONObject json;
		public void run(){
				dzialanie=true;
				while(dzialanie){
				Calendar cal = Calendar.getInstance();
				  int dayofyear = cal.get(Calendar.DAY_OF_YEAR);
				  int year = cal.get(Calendar.YEAR);
				  int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
				  int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
				  int monofmonth = cal.get(Calendar.MINUTE);
				  try {
					Thread.sleep(20000);
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("nazwa", "mm"));
					json = jsonParser.makeHttpRequest("http://opiekuj1.javaprovider.net/php/new_reservation.php",
							"POST", params);
					try {
						int r=0;
						if(json!=null){
							r=1;
						int success = json.getInt("success");
						if(success==2)
						{
							new AndroidBuildingMusicPlayerActivity(ctx,r);
							
						}
						json=null;
						}
						json=null;
					} catch (JSONException e) {
						json=null;
						//e.printStackTrace();
					}
				//	cc.execute();
				} catch (InterruptedException e) {
					json=null;
					//e.printStackTrace();
				}
		}
	}}
}
